package com.xi.controller;

import com.xi.pojo.*;
import com.xi.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/book")
@Validated
public class BookController {
    @Autowired
    private BookService bookService;

    /*
     * 书籍的分页查询
     * */
    @GetMapping
    public Result page(BookQueryParam bookQueryParam) {
        log.info("书籍分页查询 , {}", bookQueryParam);
        PageResult pageResult = bookService.page(bookQueryParam);
        return Result.success(pageResult);
    }

    /*
    * 添加书籍
    * */
    @PostMapping
    public Result add(@RequestBody Book book) {
        log.info("添加书籍 , {}", book);
        bookService.add(book);
        return Result.success();
    }

    /*
    * 书籍删除,根据id删除
    * */
    @DeleteMapping
    public Result deleteById(Integer id) {
        log.info("删除书籍 , {}", id);
        bookService.deleteById(id);
        return Result.success();
    }

    /*
     * 书籍修改
     * */
    @PutMapping
    public Result update(@RequestBody Book book) {
        log.info("修改书籍 , {}", book);
        bookService.update(book);
        return Result.success();
    }

    /*
    * 获取书籍所有类型
    * */
    @GetMapping("/classify")
    public Result listClassify() {
        log.info("获取所有书籍类型");
        return Result.success(bookService.listClassify());
    }
}
